
package com.zhongketech.sunflower.transport.util.network;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.zhongketech.sunflower.utils.ImageUtil;

import android.util.Log;

public class NetTransportUtil {

    private static final String TAG = "NetTransportUtil";

    public static InputStream getXMLContent(String requestUrl) {
        InputStream inStream = null;

        final int TIME_OUT = 8 * 1000;

        Log.d(TAG, "request ----> " + requestUrl);

        // 建立一个URL对象
        try {
            URL url = new URL(requestUrl);
            // 得到打开的链接对象
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            // 设置请求超时与请求方式
            conn.setReadTimeout(TIME_OUT);
            conn.setConnectTimeout(TIME_OUT);
            conn.setRequestMethod("GET");
            // 从链接中获取一个输入流对象
            inStream = conn.getInputStream();
            return inStream;
        } catch (Exception e) {
            Log.d(TAG, "NetTransportUtil getXMLContent url(数据读取失败！): "
                    + requestUrl);
        } finally {
            // String content = convertStreamToString(inStream);
            // Log.d(TAG, content);
        }

        return inStream;
    }

    /**
     * 从url中得到json内容
     * 
     * @param urlname
     * @return String
     */
    public static String getContent(String requestUrl) {
        String message = "";
        try {
            String authKey = getMD5(requestUrl + "aidufei");
            requestUrl += ("&authKey=" + authKey);
            message = ImageUtil.getData(requestUrl, false);
        } catch (Exception e) {
            Log.d(TAG, "NetTransportUtil getContent url(数据读取失败！): "
                    + requestUrl);
        }
        return message;

        /*
         * String message = ""; try { HttpClient client =
         * CustomerHttpClient.getHttpClient(); String authKey =
         * getMD5(requestUrl + "aidufei"); requestUrl += ("&authKey=" +
         * authKey); HttpGet request = new HttpGet(requestUrl); //
         * 发送GET请求，并将响应内容转换成字符串 HttpResponse response = client.execute(request);
         * message = EntityUtils.toString(response.getEntity(), "UTF-8"); }
         * catch (Exception e) { Log.d(TAG, "数据读取失败！getContent url: " +
         * requestUrl); } return message;
         */
    }

    /**
     * 对str进行MD5加密
     * 
     * @param str
     * @return
     */
    public static String getMD5(String str) {
        if (str == null)
            return null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            byte[] byteDigest = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < byteDigest.length; offset++) {
                i = byteDigest[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            // 32位加密
            return buf.toString();
            // 16位的加密
            // return buf.toString().substring(8, 24);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String convertStreamToString(InputStream is) {
        /*
         * To convert the InputStream to String we use the
         * BufferedReader.readLine() method. We iterate until the BufferedReader
         * return null which means there's no more data to read. Each line will
         * appended to a StringBuilder and returned as String.
         */
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "UTF-8"));
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return sb.toString();
    }
}
